<template>
  <div class="container">
    <!-- <pieChart :chartData="pieData" /> -->
    <div class="left">
      <div class="t">
        <img src="@/assets/images/waring_icon.png" />
        <span class="text-red">0</span>
      </div>
      <p class="text-white">实时报警</p>
    </div>
    <div class="piechart">
      <pieChart :chartData="pieData" />
      <div class="number" v-if="sureData.length">
        <p>{{sureData[0].value}}</p>
        <p>{{sureData[0].name}}</p>
      </div>
    </div>
    <div class="piechart">
      <pieChart :chartData="pieData2" />
    </div>
  </div>
</template>

<script>
import pieChart from '@/components/echarts/pieChart.vue'
import {getCallPoliceStutas,getCallPolice} from "@/api/visual/overview.js"

export default {
  components: { pieChart },
  data() {
    return {
      pieData2: {},
      pieData: {},
      sureData: [],
      waringData: [],
    }
  },
  mounted() {
    getCallPoliceStutas().then(res=>{
      this.sureData = [];
      res.data.map(item=>{
        this.sureData.push( { name: item.priority,value:item.num,percentage: item.percentage });
      })
      let that = this
      that.pieData = {
        legends: {
          show: false
        },
        series: [
          {
            name: '',
            type: 'pie',
            radius: [15, 30],
            center: ['25%', '50%'],
            itemStyle: {
              normal: {
                color: function (colors) {
                  var colorList = [
                    '#6DC48A',
                    '#FF452D',
                  ];
                  return colorList[colors.dataIndex];
                }
              },
            },
            label: {
              show: true,
              position: 'center',
              color: '#fff',
              formatter: function(data){ // 设置圆饼图中间文字排版
                return that.sureData[0].percentage
              }
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 12,
              }
            },
            data: that.sureData,
            animationEasing: 'cubicInOut',
            animationDuration: 2600,
            // labelLine: false,
          }
        ],
      }
    })
    getCallPolice().then(res=>{
      this.waringData = [];
      res.data.map(item=>{
        this.waringData.push( { name: item.priority + '级',value:item.num,percentage: item.percentage });
      })
      let that = this
      this.pieData2 = {
        legends: {
          itemGap: 0,
          itemHeight: 20,
          itemWidth: 0,
          left: '45%',
          bottom: 'center',
          data: [{
            name: 'Ⅰ级',
            textStyle: {
              color: '#FF452D',
            }
          }, {
            name: 'Ⅱ级',
            textStyle: {
              color: '#FFD15C',
            }
          }, {
            name: 'Ⅲ级',
            textStyle: {
              color: '#07D2FA',
            }
          }],
          textStyle: {
            rich: {
              a: {
                fontSize: 14,
                fontWeight: 400,
                color: 'auto',
                align: 'left',
                padding: [0, 0, 0, 0],
              },
              b: {
                padding: [0, 0, 0, 5],
                fontSize: 14,
                color: 'auto',
                align: 'right',
                lineHeight: 25,
              }
            }
          },
          formatter: function (name) {
            var data = that.waringData;//获取series中的data
            var tarValue;
            for (var i = 0, l = data.length; i < l; i++) {
              if (data[i].name == name) {
                tarValue = data[i].value;
              }
            }
            var arr = ['{a|' + name + '}{b|' + tarValue + '}{a|}']
            return arr.join('\n')
          },
        },
        series: [
          {
            name: '',
            type: 'pie',
            radius: [15, 30],
            center: ['25%', '50%'],
            itemStyle: {
              normal: {
                color: function (colors) {
                  var colorList = [
                    '#FF452D',
                    '#FFD15C',
                    '#07D2FA'
                  ];
                  return colorList[colors.dataIndex];
                }
              },
            },
            data: that.waringData,
            animationEasing: 'cubicInOut',
            animationDuration: 2600,
            labelLine: false,
          }
        ]
      }
    })
  }
}
</script>

<style lang="scss" scoped>
.container {
  height: 8.7vh;
  background: linear-gradient(180deg, rgba(2, 68, 102, 0.3) 0%, rgba(2, 60, 89, 0.3) 0%, rgba(1, 18, 27, 0.3) 100%);
  border: 2px solid rgba(31, 198, 255, 0.15);
  padding: 0 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .left {
    width: 25%;

    .t {
      display: flex;
      align-items: center;

      img {
        width: 30px;
        height: 30px;
      }

      span {
        font-size: 20px;
        font-weight: bold;
        margin-left: 10px;
      }
    }

    p {
      font-size: 12px;
      line-height: 20px;
      margin-top: 5px;
    }
  }

  .piechart {
    width: 35%;
    height: 100%;
    position: relative;
    display: flex;

    .number {
      position: absolute;
      right: 10%;
      top: 50%;
      transform: translateY(-50%);
      color: #fff;
      font-size: 14px;
      text-align: center;

      p {
        margin: 5px 0;
      }
    }
  }
}
</style>
